/*
 * 操作SD卡的实用工具
 */

package com.mg.g2048.SD;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import com.mg.g2048.Data.AppConfig;
import com.mg.g2048.Data.DataAssistant;

public class SDUtils {
	// SD卡的路径
	private static String SDPath = null;
	// 通用数据处理助手
	private static DataAssistant<String> dataAssistant = null;

	// 初始化
	static {
		init();
	}
	
	/**
	 * 将GZIP文件解压（输出文件存在时会覆盖）
	 * 
	 * @param inGzipFilePath
	 *            输入GZIP文件路径
	 * @param outFilePath
	 *            输出文件路径
	 * @return 输出文件的File，出错返回null
	 */
	public static File decompressByGzip(String inGzipFilePath, String outFilePath) {
		if (inGzipFilePath == null || outFilePath == null)
			return null;
		
		if (!exists(inGzipFilePath)) {
			Log.i(AppConfig.Tag.FILE, "Input GZIP file NOT exist: " + inGzipFilePath);
			return null;
		}
		
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(inGzipFilePath);
		} catch (FileNotFoundException e) {
			Log.i(AppConfig.Tag.FILE, "Input GZIP file NOT exist: " + inGzipFilePath);
			e.printStackTrace();
		}
		if (fileInputStream == null)
			return null;
		
		File file = decompressByGzip(fileInputStream, outFilePath);
		try {
			fileInputStream.close();
		} catch (IOException e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing data!");
			e.printStackTrace();
		}
		
		return file;
	}

	/**
	 * 将GZIP数据流解压到文件（输出文件存在时会覆盖）
	 * 
	 * @param inputStream
	 *            GZIP数据流
	 * @param outFilePath
	 *            输出文件路径
	 * @return 输出文件的File，出错返回null
	 */
	public static File decompressByGzip(InputStream inputStream, String outFilePath) {
		if (inputStream == null || outFilePath == null)
			return null;
		
		GZIPInputStream gzipInputStream = null;
		try {
			gzipInputStream = new GZIPInputStream(inputStream);
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "ERROR when decompressing file by GZIP!");
			e.printStackTrace();

			return null;
		}
		if (gzipInputStream == null)
			return null;

		File file = null;
		// 该文件已存在，则直接获取其File，覆盖之
		if (exists(outFilePath)) {
			file = new File(outFilePath);
		} else {
			file = createFile(outFilePath);
		}
		if (file == null)
			return null;

		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			if (fileOutputStream == null)
				return null;

			int temp = 0;
			byte[] buffer = new byte[1024];
			while ((temp = gzipInputStream.read(buffer)) != -1)
				fileOutputStream.write(buffer, 0, temp);

			fileOutputStream.flush();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing data!");
			e.printStackTrace();

			return null;
		} finally {
			try {
				gzipInputStream.close();
				fileOutputStream.close();
			} catch (Exception e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * 将文件压缩为GZIP格式（输出文件存在时会覆盖）
	 * 
	 * @param inFilePath
	 *            输入文件路径
	 * @param outGzipFilePath
	 *            输出GZIP文件路径
	 * @return 输出GZIP文件的File，出错返回null
	 */
	public static File compressByGzip(String inFilePath, String outGzipFilePath) {
		if (inFilePath == null || outGzipFilePath == null)
			return null;
		
		if (!exists(inFilePath)) {
			Log.i(AppConfig.Tag.FILE, "Input file NOT exist: " + inFilePath);
			return null;
		}

		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(inFilePath);
		} catch (FileNotFoundException e) {
			Log.i(AppConfig.Tag.FILE, "Input file NOT exist: " + inFilePath);
			e.printStackTrace();

			return null;
		}
		if (fileInputStream == null)
			return null;

		File file = compressByGzip(fileInputStream, outGzipFilePath);
		try {
			fileInputStream.close();
		} catch (IOException e) {
			Log.i(AppConfig.Tag.FILE, "Error when closing!");
			e.printStackTrace();
		}

		return file;
	}

	/**
	 * 将输入数据流压缩为GZIP格式文件（输出文件存在时会覆盖）
	 * 
	 * @param inputStream
	 *            输入数据流
	 * @param outGzipFilePath
	 *            输出GZIP文件路径
	 * @return 输出GZIP文件的File，出错返回null
	 */
	public static File compressByGzip(InputStream inputStream, String outGzipFilePath) {
		if (inputStream == null || outGzipFilePath == null)
			return null;
		
		File file = null;
		// 该文件已存在，则直接获取其File，覆盖之
		if (exists(outGzipFilePath)) {
			file = new File(outGzipFilePath);
		} else {
			file = createFile(outGzipFilePath);
		}
		if (file == null)
			return null;

		GZIPOutputStream gzipOutputStream = null;
		try {
			gzipOutputStream = new GZIPOutputStream(new FileOutputStream(file));
			if (gzipOutputStream == null)
				return null;

			int temp = 0;
			byte[] buffer = new byte[1024];
			while ((temp = inputStream.read(buffer)) != -1)
				gzipOutputStream.write(buffer, 0, temp);

			gzipOutputStream.flush();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing data!");
			e.printStackTrace();

			return null;
		} finally {
			try {
				gzipOutputStream.close();
			} catch (Exception e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * 将 二维HashMap 的数据写入到文件中
	 * 
	 * @param hashMap2D
	 *            二维HashMap数据
	 * @param filePath
	 *            文件路径
	 * @param sortKey
	 *            是否对Key排序
	 * @param separator
	 *            分隔符
	 * @param characterSet
	 *            字符集名称
	 */
	public static void write2DHashMapToFile(HashMap<String, ArrayList<String>> hashMap2D, String filePath, boolean sortKey,
			String separator, String characterSet) {
		Log.i(AppConfig.Tag.FILE, "write2DHashMapToFile...");
		if (hashMap2D == null || filePath == null || separator == null || characterSet == null)
			return;
		if (hashMap2D.isEmpty())
			return;

		// 一维 HashMap
		HashMap<String, String> hashMap = new HashMap<String, String>();
		// 二维HashMap的Key
		Set<String> keySet = hashMap2D.keySet();

		int i = 0;
		int count = 0;
		String integratedStr = null;
		ArrayList<String> arrayList = null;

		// 将二维变成一维，放入一维 HashMap
		for (String key : keySet) {
			arrayList = hashMap2D.get(key);
			if (arrayList == null)
				continue;

			count = arrayList.size();
			if (count <= 0)
				continue;

			for (i = 0; i < count; i++) {
				// 第一个 Object
				if (i == 0) {
					integratedStr = arrayList.get(i);
				} else {
					// 以 separator 相连
					integratedStr += (separator + arrayList.get(i));
				}
			}

			// 添加到 一维 HashMap
			hashMap.put(key, new String(integratedStr));
		}

		// 将 一维 HashMap 的数据写入到文件中
		writeHashMap2File(hashMap, filePath, sortKey, separator, characterSet);
	}

	/**
	 * 将 HashMap 的数据写入到文件中
	 * 
	 * @param hashMap
	 *            HashMap数据
	 * @param filePath
	 *            文件路径
	 * @param sortKey
	 *            是否对Key排序
	 * @param separator
	 *            分隔符
	 * @param characterSet
	 *            字符集名称
	 */
	public static void writeHashMap2File(HashMap<String, String> hashMap, String filePath, boolean sortKey, String separator,
			String characterSet) {
		Log.i(AppConfig.Tag.FILE, "writeHashMap2File...");
		if (hashMap == null || filePath == null || separator == null || characterSet == null)
			return;
		if (hashMap.isEmpty())
			return;

		if (!exists(filePath))
			createFile(filePath);

		Set<String> keySet = hashMap.keySet();
		String[] keyArray = new String[keySet.size()];
		keySet.toArray(keyArray);

		// 排序
		if (sortKey)
			Arrays.sort(keyArray);

		PrintWriter printWriter = null;
		String valueStr = null;
		try {
			printWriter = new PrintWriter(filePath, characterSet);
			if (printWriter == null)
				return;

			for (String key : keyArray) {
				valueStr = hashMap.get(key);
				if (valueStr == null)
					continue;
				if (valueStr.length() <= 0)
					continue;

				printWriter.println(key + separator + valueStr + "\r");
			}

			printWriter.flush();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing HashMap data to File: " + filePath);
			e.printStackTrace();
		} finally {
			try {
				printWriter.close();
			} catch (Exception e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 将文件的数据读取到一个二维 HashMap 中
	 * 
	 * @param filePath
	 *            文件路径
	 * @param separator
	 *            数据之间的分割符
	 * @param characterSet
	 *            字符集名称
	 * @return 二维 HashMap
	 */
	public static HashMap<String, ArrayList<String>> read2DHashMapFromFile(String filePath, String separator, String characterSet) {
		if (filePath == null || separator == null || characterSet == null)
			return null;
		if (!exists(filePath))
			return null;
		
		Log.i(AppConfig.Tag.FILE, "read2DHashMapFromFile...");
		HashMap<String, ArrayList<String>> hashMap2D = new HashMap<String, ArrayList<String>>();
		
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), characterSet));
			if (bufferedReader == null)
				return null;
			
			String line = null;
			String[] lineArray = null;
			int i = 0;
			
			while ((line = bufferedReader.readLine()) != null) {
				// 以 # 或 ; 或 //开头的行
				if (line.matches("^[#;/]"))
					continue;
				
				// 去掉回车符
				line = dataAssistant.removeEntersFromStr(line);
				// 去掉首尾空字符
				line.trim();
				if (line.length() <= 0)
					continue;
				
				lineArray = line.split(separator);
				if (lineArray == null)
					continue;
				// 至少有2列数据
				if (lineArray.length < 2)
					continue;
				
				ArrayList<String> arrayList = new ArrayList<String>();
				// 从第2列数据开始
				for (i = 1; i < lineArray.length; i++)
					arrayList.add(lineArray[i]);
				
				hashMap2D.put(lineArray[0], arrayList);
			}
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when reading 2D HashMap from file: " + filePath);
			e.printStackTrace();
		} finally {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}
		if (hashMap2D.isEmpty())
			return null;
		
		return hashMap2D;
	}
	
	/**
	 * 将二维链表的数据写入到文件中
	 * 
	 * @param list2D
	 *            二维链表
	 * @param filePath
	 *            文件路径
	 * @param separator
	 *            分隔符
	 * @param characterSet
	 *            字符集名称
	 */
	public static void write2DListToFile(LinkedList<LinkedList<String>> list2D, String filePath, String separator,
			String characterSet) {
		Log.i(AppConfig.Tag.FILE, "write2DListToFile...");
		if (list2D == null || filePath == null || separator == null || characterSet == null)
			return;
		if (list2D.isEmpty())
			return;

		int i = 0;
		int count = 0;
		String integratedStr = null;
		// 一维链表
		LinkedList<String> list = new LinkedList<String>();

		// 将二维变成一维，放入 一维链表
		for (LinkedList<String> tempList : list2D) {
			if (tempList == null)
				continue;

			count = tempList.size();
			if (count <= 0)
				continue;

			for (i = 0; i < count; i++) {
				// 第一个
				if (i == 0) {
					integratedStr = tempList.get(i);
				} else {
					// 以 separator 相连
					integratedStr += (separator + tempList.get(i));
				}
			}

			// 添加到 一维链表
			list.addLast(new String(integratedStr));
		}

		// 将 一维链表 的数据写入到文件中
		writeListToFile(list, filePath, separator, characterSet);
	}

	/**
	 * 将链表的数据写入到文件中
	 * 
	 * @param list
	 *            链表
	 * @param filePath
	 *            文件路径
	 * @param separator
	 *            分隔符
	 * @param characterSet
	 *            字符集名称
	 */
	public static void writeListToFile(LinkedList<String> list, String filePath, String separator,
			String characterSet) {
		Log.i(AppConfig.Tag.FILE, "writeListToFile...");
		if (list == null || filePath == null || separator == null || characterSet == null)
			return;
		if (list.isEmpty())
			return;

		if (!exists(filePath))
			createFile(filePath);

		PrintWriter printWriter = null;
		try {
			printWriter = new PrintWriter(filePath, characterSet);
			if (printWriter == null)
				return;

			for (String item : list) {
				if (item == null)
					continue;
				if (item.length() <= 0)
					continue;

				printWriter.println(item + "\r");
			}

			printWriter.flush();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing list data to File: " + filePath);
			e.printStackTrace();
		} finally {
			try {
				printWriter.close();
			} catch (Exception e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}
	}

	/**
	 * 将文件的数据读取到一个二维链表中
	 * 
	 * @param filePath
	 *            文件路径
	 * @param separator
	 *            数据之间的分割符
	 * @param characterSet
	 *            字符集名称
	 * @return 二维链表
	 */
	public static LinkedList<LinkedList<String>> read2DListFromFile(String filePath, String separator, String characterSet) {
		if (filePath == null || separator == null || characterSet == null)
			return null;
		if (!exists(filePath))
			return null;

		Log.i(AppConfig.Tag.FILE, "read2DListFromFile...");
		LinkedList<LinkedList<String>> list2D = new LinkedList<LinkedList<String>>();

		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), characterSet));
			if (bufferedReader == null)
				return null;

			String line = null;
			String[] lineArray = null;
			int i = 0;

			while ((line = bufferedReader.readLine()) != null) {
				// 以 # 或 ; 或 //开头的行
				if (line.matches("^[#;/]"))
					continue;

				// 去掉回车符
				line = dataAssistant.removeEntersFromStr(line);
				// 去掉首尾空字符
				line.trim();
				if (line.length() <= 0)
					continue;

				lineArray = line.split(separator);
				if (lineArray == null)
					continue;

				LinkedList<String> list = new LinkedList<String>();
				for (i = 0; i < lineArray.length; i++)
					list.addLast(lineArray[i]);

				if (!list.isEmpty())
					list2D.addLast(list);
			}
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when reading 2D List from file: " + filePath);
			e.printStackTrace();
		} finally {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}
		if (list2D.isEmpty())
			return null;

		return list2D;
	}

	/**
	 * 从文件获取图片的Bitmap数据
	 * 
	 * @param filePath
	 *            文件路径
	 * @return 图片的Bitmap数据。路径无效或解析文件出错，则返回null
	 */
	public static Bitmap getBitmapFromFile(String filePath) {
		Log.i(AppConfig.Tag.FILE, "getBitmapFromFile: " + filePath);
		if (filePath == null)
			return null;
		if (!exists(filePath))
			return null;

		return BitmapFactory.decodeFile(filePath);
	}

	/**
	 * 将图片的Bitmap数据写入文件中
	 * 
	 * @param bmp
	 *            图片的Bitmap数据
	 * @param filePath
	 *            文件路径
	 * @param quality
	 *            保存图片质量
	 */
	public static void writeBitmap2File(Bitmap bmp, String filePath, int quality) {
		Log.i(AppConfig.Tag.FILE, "writeBitmap2File: " + filePath);
		if (bmp == null || filePath == null || quality <= 0 || quality > 100)
			return;

		File file = null;
		// 文件已存在，则覆盖
		if (exists(filePath))
			file = new File(filePath);
		// 新建缓存文件
		else
			file = createFile(filePath);
		if (file == null)
			return;

		try {
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
			if (bufferedOutputStream == null)
				return;

			// PNG格式
			if (filePath.endsWith(".png"))
				bmp.compress(Bitmap.CompressFormat.PNG, quality, bufferedOutputStream);
			// JPEG或JPG格式
			else
				bmp.compress(Bitmap.CompressFormat.JPEG, quality, bufferedOutputStream);

			// 刷新并关闭流
			bufferedOutputStream.flush();
			bufferedOutputStream.close();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when new cache file, please check the path or your privileges!");
			e.printStackTrace();
		}
	}

	/**
	 * 将文本数据流写入文件（文件存在时会覆盖）
	 * 
	 * @param inputStream
	 *            多媒体数据流
	 * @param filePath
	 *            文件路径
	 * @return 该文件的File，出错返回null
	 */
	public static File writeTextData2File(InputStream inputStream, String filePath) {
		if (inputStream == null || filePath == null)
			return null;

		File file = null;
		// 该文件已存在，则直接获取其File，覆盖之
		if (exists(filePath))
			file = new File(filePath);
		else
			file = createFile(filePath);
		if (file == null)
			return null;

		BufferedReader bufferedReader = null;
		PrintWriter printWriter = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			printWriter = new PrintWriter(file);
			if (bufferedReader == null || printWriter == null)
				return null;

			String line = null;
			while ((line = bufferedReader.readLine()) != null)
				printWriter.println(line);

			printWriter.flush();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing data!");
			e.printStackTrace();

			return null;
		} finally {
			try {
				printWriter.close();
				bufferedReader.close();
			} catch (Exception e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * 将多媒体数据流写入文件（文件存在时会覆盖）
	 * 
	 * @param inputStream
	 *            多媒体数据流
	 * @param filePath
	 *            文件路径
	 * @return 该文件的File，出错返回null
	 */
	public static File writeMediaData2File(InputStream inputStream, String filePath) {
		if (inputStream == null || filePath == null)
			return null;

		File file = null;
		// 该文件已存在，则直接获取其File，覆盖之
		if (exists(filePath))
			file = new File(filePath);
		else
			file = createFile(filePath);
		if (file == null)
			return null;

		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			if (fileOutputStream == null)
				return null;

			int temp = 0;
			byte[] buffer = new byte[1024];
			while ((temp = inputStream.read(buffer)) != -1)
				fileOutputStream.write(buffer, 0, temp);

			fileOutputStream.flush();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when writing data!");
			e.printStackTrace();

			return null;
		} finally {
			try {
				fileOutputStream.close();
			} catch (Exception e) {
				Log.i(AppConfig.Tag.FILE, "Error when closing!");
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * 自底向上取路径上一定级数的目录名称，合成一段名称
	 * 
	 * @param path
	 *            路径
	 * @param levelNeed
	 *            需要取的级数
	 * @return 一段合成好的名称
	 */
	public static String getPathSomeLevelAsName(String path, int levelNeed) {
		if (path == null || levelNeed < 1)
			return null;
		if (path.length() == 0)
			return null;

		// 将path分割后，各级目录名称
		String[] dirs = path.split("/+");
		if (dirs == null)
			return null;

		// 是否是第1段
		boolean isFirstSection = true;
		String resultName = "";

		// 自底向上取原路径上一定级数的目录名称，合成一段名称
		for (int i = levelNeed; i > 0; i--) {
			// 没有要求数量的级数，则降低要求少取级数
			if (dirs.length - i < 0)
				continue;

			if (isFirstSection) {
				// 第1段直接添加
				resultName += dirs[dirs.length - i];
				isFirstSection = false;
			} else {
				// 后续段先加"_"
				resultName += ("_" + dirs[dirs.length - i]);
			}
		}

		return resultName;
	}

	/**
	 * 获取SD卡上该路径的全路径
	 * 
	 * @param path
	 *            SD卡上的路径
	 * @return 全路径
	 */
	public static String getFullPath(String path) {
		if (path == null)
			return null;

		// 没有正确初始化
		if (!gotValidSDPath())
			init();
		
		return SDPath + "/" + path;
	}

	/**
	 * 删除文件或目录
	 * 
	 * @param file
	 *            文件或目录的 File
	 * @param delDir
	 *            若是目录是否删除
	 */
	public static void delete(File file, boolean delDir) {
		if (file == null)
			return;

		try {
			if (!file.exists()) {
				Log.i(AppConfig.Tag.FILE, "This file/directory is not existed: " + file.getCanonicalPath());
				return;
			}

			// 是文件
			if (file.isFile()) {
				file.delete();
			}
			// 是目录
			else if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++)
					delete(files[i], delDir);

				// 要求删除目录
				if (delDir)
					file.delete();
			}
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when deleting file/directory, please check your privileges!");
			e.printStackTrace();
		}
	}

	/**
	 * 新建目录
	 * 
	 * @param dirPath
	 *            目录路径
	 * @return 该目录的File，出错返回null
	 */
	public static File createDirs(String dirPath) {
		if (dirPath == null)
			return null;
		
		File dir = null;
		try {
			dir = new File(dirPath);
			dir.mkdirs();
		} catch (Exception e) {
			Log.i(AppConfig.Tag.FILE, "Error when creating directory(s): " + dirPath);
			e.printStackTrace();
		}

		return dir;
	}

	/**
	 * 新建文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @return 该文件的File，出错返回null
	 */
	public static File createFile(String filePath) {
		if (filePath == null)
			return null;
		
		File file = null;
		try {
			file = new File(filePath);
			file.createNewFile();
		} catch (IOException e) {
			Log.i(AppConfig.Tag.FILE, "Error when creating file: " + filePath);
			e.printStackTrace();
		}

		return file;
	}

	/**
	 * 判断文件或目录是否存在
	 * 
	 * @param path
	 *            文件或目录的路径
	 * @return 存在时返回true，不存在时返回false
	 */
	public static boolean exists(String path) {
		if (path == null)
			return false;

		File file = new File(path);
		return file.exists();
	}

	/**
	 * 判断是否获取到了有效的SD卡路径
	 * 
	 * @return 有效时返回true，无效时返回false
	 */
	public static boolean gotValidSDPath() {
		if (SDPath == null)
			return false;

		return true;
	}

	/**
	 * 获取SD卡的路径
	 * 
	 * @return SD卡的路径
	 */
	public static String getSDPath() {
		// 没有正确初始化
		if (!gotValidSDPath())
			init();
		
		return SDPath;
	}

	/**
	 * 初始化
	 */
	public static void init() {
		Log.i(AppConfig.Tag.FILE, "Setting SDPath...");

		// SD卡插入，则获得其路径
		if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
			try {
				SDPath = Environment.getExternalStorageDirectory().getCanonicalPath();
			} catch (IOException e) {
				Log.i(AppConfig.Tag.FILE, "ERROR when setting SDPath!");
				e.printStackTrace();
			}
		}
		// SD卡未插入
		else {
			Log.i(AppConfig.Tag.FILE, "SD mounting ERROR!");
		}

		// 通用数据处理助手
		dataAssistant = new DataAssistant<String>();
	}
	
}
